﻿@model WeddingEventManagementApp.Models.SettingsModel
@{
    ViewBag.Title = "Site Settings";
    var groupedSettings = Model.Settings.GroupBy(i => i.Group);
}


<div class="twelve columns">
    <h3>Site Settings</h3>
    <dl class="mobile tabs">
        <dd class="active"><a href="#tab1">Application Settings</a></dd>
        <dd><a href="#tab2">Application Users</a></dd>
        <dd><a href="#tab3">Event Types</a></dd>
        <dd><a href="#tab4">Packages</a></dd>
    </dl>

    <ul class="tabs-content">
        <li id="tab1Tab" class="active twelve column">

                @using (Html.BeginForm("Index", "Setting", FormMethod.Post, new { data_confirm = "change system settings" }))
                {
                    int i = 0; //init i
                    
                    foreach (var group in groupedSettings)
                    {
                        <h4>@group.Key</h4>
                    
                        foreach (var setting in group)
                        {
                            
                            <div class="row">
                                @Html.HiddenFor(modelItem => Model.Settings[i].key)
                                @Html.HiddenFor(modelItem => Model.Settings[i].DataType)
                                @Html.HiddenFor(modelItem => Model.Settings[i].Description)

                                <div class="three column">
                                    @Html.LabelFor(modelItem => Model.Settings[i].value, Model.Settings[i].Description, new { @class = "text-right", style = "font-weight: bold;" })
                                </div>

                                <div class="six column">

                                    @if (Model.Settings[i].DataType == DataTypes.LongText.ToString())
                                    {
                                        @Html.TextAreaFor(modelItem => Model.Settings[i].value, new { rows = "10" })
                                    }
                                    else if (Model.Settings[i].DataType == DataTypes.Currency.ToString())
                                    {
                                        @Html.TextBoxFor(modelItem => Model.Settings[i].value, new { type = "number" })
                                    }
                                    else if (Model.Settings[i].DataType == DataTypes.Decimal.ToString())
                                    {
                                        @Html.TextBoxFor(modelItem => Model.Settings[i].value, new { type = "number" })
                                    }
                                    else if (Model.Settings[i].DataType == DataTypes.Color.ToString())
                                    {
                                        @Html.TextBoxFor(modelItem => Model.Settings[i].value, new { @class = "miniColor" })
                                    }
                                    else
                                    {
                                        @Html.TextBoxFor(modelItem => Model.Settings[i].value)
                                    }
                                </div>

                                <div class="three column">
                                    @Html.ValidationMessageFor(modelItem => Model.Settings[i].value)
                                </div>
                            </div>
                    
                            i++; //Increment i
                        }
                    }

                    <hr />
                    <div class="row">
                        <div class="eight column offset-by-four">
                            <input type="submit" class="button" value="Save Settings" />
                        </div>
                    </div>
                }
        </li>
        <li id="tab2Tab" class="twelve column">
            <div class="twelve columns">
                <h4 style="display: inline-block"><i class="icon-users"></i>Users</h4>
                <span class="label round success" style="margin-left: 20px;">
                    <a data-reveal-id="new-user"  style="cursor: pointer; color: white;">Add New</a>
                </span>
            </div>

            <table id="users-table">
                <thead>
                    <tr>
                        <th class="xlarge">User</th>
                        <th class="large">Email</th>
                        <th class="large">Role</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var user in Model.Users)
                    {
                        <tr>
                            <td class="xlarge">
                                <img src="@string.Format(@"https://www.gravatar.com/avatar/{0}?s={1}&d=mm", user.Email == null ? "" : WeddingEventManagementApp.Utility.MD5Hash(user.Email), 30)" />
                                @user.UserName
                            </td>
                            <td class="large">
                                @user.Email
                            </td>
                            <td>
                                @if (@user.IsAdmin)
                                {
                                    <text>Administrator</text>
                                }
                                else
                                {
                                    <text>User</text>
                                }
                            </td>
                            <td class="large">
                                @if (!@user.IsAdmin)
                                {
                                    using (Html.BeginForm("MakeAdmin", "Setting", new { username = user.UserName }, FormMethod.Post, new { data_confirm = "make " + user.UserName + " an admin" }))
                                    {
                                    <input type="submit" value="Make User Admin" />
                                    }
                                }
                                else
                                {
                                    using (Html.BeginForm("RevokeAdmin", "Setting", new { username = user.UserName }, FormMethod.Post, new { data_confirm = "revoke " + user.UserName + "'s admin status" }))
                                    {
                                    <input type="submit" value="Revoke Admin" />
                                    }
                                } |
                                    @using (Ajax.BeginForm("ResetPassword", "Setting", new { username = user.UserName }, new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "reset-text", OnSuccess = "$('#reset-password').reveal();" }, new { data_confirm = "reset " + user.UserName + "'s password" }))
                                    {
                                        <input type="submit" value="Reset User Password" />
                                    } |
                                    @using (Html.BeginForm("DeleteUser", "Setting", new { username = user.UserName }, FormMethod.Post, new { data_confirm = "delete " + user.UserName }))
                                    {
                                        <input type="submit" value="Delete User" />
                                    }
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </li>
        <li id="tab3Tab" class="twelve column">
            <div class="twelve columns">
                <h4 style="display: inline-block">Event Types</h4>
                <span class="label round success" style="margin-left: 20px;">
                    <a data-reveal-id="new-event-type"  style="cursor: pointer; color: white;">Add New</a>
                </span>
            </div>
            
            <div class="twelve columns">
                <ul class="no-bullet">
                    @foreach (var item in Model.EventTypes)
                    {
                        <li>
                            <span style="width: 200px; display: inline-block">@item.EventTypeName</span>
                            <span class="round label" style="color: #fff ; background: @item.CalendarColor">@item.CalendarColor</span>
                        </li>
                    }
                </ul>
            </div>
        </li>
        <li id="tab4Tab" class="twelve column">
            <div class="twelve columns">
                <h4 style="display: inline-block">Packages</h4>
                <span class="label round success" style="margin-left: 20px;">
                    <a data-reveal-id="new-package" style="cursor: pointer; color: white;">Add New</a>
                </span>
            </div>

            <div class="twelve columns">
                <ul class="no-bullet">
                @foreach (var item in Model.Packages)
                {
                    <li>
                        <span style="width: 200px; display: inline-block">@item.PackageType</span>
                        <span style="width: 200px; display: inline-block">@string.Format("{0:C}", item.BasePrice)</span>
                        <span class="round label" style="color: #fff ; background: @item.Color">@item.Color</span>
                    </li>
                }
                </ul>
            </div>
        </li>
    </ul>
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <div id="reset-password" class="reveal-modal">
        <h4>Reset Password</h4>
        <p id="reset-text"></p>
        <a class="close-reveal-modal">&#215;</a>
    </div>

    <div id="new-user" class="reveal-modal">
        @{Html.RenderPartial("_CreateUser", new WeddingEventManagementApp.Models.RegisterModel());}
        <a class="close-reveal-modal">&#215;</a>
    </div>

    <div id="new-event-type" class="reveal-modal">
        @{Html.RenderPartial("_CreateEventType", new EventType());}
        <a class="close-reveal-modal">&#215;</a>
    </div>

    <div id="new-package" class="reveal-modal">
        @{Html.RenderPartial("_CreatePackage", new Package());}
        <a class="close-reveal-modal">&#215;</a>
    </div>

    <script type="text/javascript">
        $('.miniColor').miniColors();

        jQuery('#users-table form').submit(function (e) {
            var text = $(this).data('confirm');
            var r = confirm("Are you sure you want to " + text + "?" );
            if (r === false) {
                e.preventDefault();
            }
        });
    </script>
}
